package com.siemens.industrial.web.mvc.service.partner;

import com.siemens.industrial.model.partner.ProductInfo;
import com.siemens.industrial.model.partner.User;
import com.siemens.industrial.utils.GlobalResponseCode;
import com.siemens.industrial.utils.RestResponse;
import com.siemens.industrial.web.mvc.dao.partner.PartnerDeviceDao;
import com.siemens.industrial.web.mvc.dao.partner.PartnerProductInfoDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by qupen on 2016/11/8.
 */
@Service
public class PartnerProductService {
    @Resource
    PartnerProductInfoDao partnerProductInfoDao;

    @Resource
    PartnerDeviceDao partnerDeviceDao;

    public Map<String, Object> saveProduct(String tenantId, List<Object> data, User user) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (Object product : data) {
            String deviceId = ((Map) product).get("deviceId").toString();
            String time = ((Map) product).get("date").toString();
            String startTime = ((Map) product).get("startTime").toString();
            String endTime = ((Map) product).get("endTime").toString();

            ProductInfo productInfo = partnerProductInfoDao.getOne(tenantId, deviceId, time+" "+startTime);

            if (productInfo != null) {
                updateProduct(tenantId, product, user);
                continue;
            }

//            boolean result = partnerProductInfoDao.findDeviceState(tenantId, startTime, endTime, deviceId);

//            if (result) {
                String userId = user.getUserId();
                int productNum = Integer.parseInt(((Map) product).get("num").toString());
                int goodNum = Integer.parseInt(((Map) product).get("good").toString());
                int badNum = Integer.parseInt(((Map) product).get("bad").toString());
                double rate = Double.parseDouble(((Map) product).get("rate").toString());
                String lead = ((Map) product).get("shiftLead").toString();
                int i = partnerProductInfoDao.insertProductInfo(tenantId, deviceId,time, startTime, endTime, userId, productNum, goodNum, badNum, rate,lead);
                if (i != 1) {
                    return RestResponse.create(GlobalResponseCode.ERROR_PRODUCT_CREATE_FAIL)
                            .putData(new HashMap<String, Object>() {{
                                put("startTime", startTime);
                                put("endTime", endTime);
                            }}).build();
                }
//            } else {
//                return RestResponse.create(GlobalResponseCode.ERROR_STATE_NOT_EXISTED_OR_NOT_WORK)
//                        .putData(new HashMap<String, Object>() {{
//                            put("startTime", startTime);
//                            put("endTime", endTime);
//                        }})
//                        .build();
//
//            }
        }
        partnerProductInfoDao.executeStoredProcedure(tenantId,((Map)data.get(0)).get("deviceId").toString(),((Map) data.get(0)).get("date").toString());
        return RestResponse.create(GlobalResponseCode.SUCCESS).build();
    }

    public Map<String, Object> updateProduct(String tenantId, Object product, User user) {

        String deviceId = ((Map) product).get("deviceId").toString();
        String startTime = ((Map) product).get("date").toString() + " " + ((Map) product).get("startTime").toString();
        String userId = user.getUserId();
        int productNum = Integer.parseInt(((Map) product).get("num").toString());
        int goodNum = Integer.parseInt(((Map) product).get("good").toString());
        int badNum = Integer.parseInt(((Map) product).get("bad").toString());
        double rate = Double.parseDouble(((Map) product).get("rate").toString());
        String lead = ((Map) product).get("shiftLead").toString();

        int i = partnerProductInfoDao.update(tenantId, deviceId, startTime, userId, productNum, goodNum, badNum, rate,lead);

        if (i == 1) {
            return RestResponse.create(GlobalResponseCode.SUCCESS).build();
        }

        return RestResponse.create(GlobalResponseCode.ERROR_PRODUCT_MODIFY_FAIL).build();
    }

    public List<Map<String, Object>> getAllProduct(String tenantId, String deviceId) {
        return partnerProductInfoDao.getAll(tenantId, deviceId);
    }

    public List<Map<String, Object>> getTimes(String tenantId, String deviceId) {
        return partnerProductInfoDao.findStartTimes(tenantId, deviceId);
    }

    public Map<String, Object> getOneProduct(String tenantId, String deviceId, String startTime) {
        return partnerProductInfoDao.getOneHaveUniqueId(tenantId, deviceId, startTime);
    }

    public List<Map<String, Object>> findServicesByTime(String database, String startTime, String endTime, String deviceId) {
        startTime = startTime.replace("/", "-");
        endTime = endTime.replace("/", "-");
        return partnerProductInfoDao.findServicesByTime(database, startTime, endTime, deviceId);
    }

    public List<String> findAWeekTime(String database, Date time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.add(Calendar.DAY_OF_WEEK, -6);
        String startTime = sdf.format(calendar.getTime());
        String endTime = sdf.format(time);
        return partnerProductInfoDao.findAWeekTime(database, startTime, endTime);
    }

    public List<Map<String, Object>> findDeviceAndProduct(String database, String startTime, String endTime) {
        List<Map<String, Object>> device = partnerDeviceDao.findAllDeviceExceptDelete(database);
        List<Map<String, Object>> productState = partnerProductInfoDao.findProductByDay(database, startTime, endTime);
        List<Map<String, Object>> list = new ArrayList<>();
        for (int i = 0; i < device.size(); i++) {
            String deviceId = device.get(i).get("device_id").toString();
            Map<String, Object> map = new HashMap<>();
            map.put("uniqueId", device.get(i).get("device_unique_id").toString());
            map.put("deviceId", deviceId);
            for (int m = 0; m < productState.size(); m++) {
                if (productState.get(m).get("deviceId").toString().equals(deviceId)) {
                    map.put(productState.get(m).get("date").toString(), productState.get(m).get("state").toString());
                }
            }
            list.add(map);
        }
        return list;
    }

    public List<Map<String, Object>> findShiftByDeviceIdAndTime(String database, String time, String deviceId){
        return partnerProductInfoDao.findShiftByDeviceIdAndTime(database, time, deviceId);
    }
}
